package org.qufenqi.api.controller.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.qufenqi.api.common.CommonResponse;
import org.qufenqi.api.entity.Address;
import org.qufenqi.api.entity.Instalment;
import org.qufenqi.api.service.BuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("/buy")
public class BuyController {
    @Autowired
    private BuyService buyService;

    //获取用户的地址
    @PostMapping("/getaddress")
    @ResponseBody
    public CommonResponse<Address> getAddress(@RequestBody String id){
        JSONObject jsonObject = JSON.parseObject(id);
        String k = jsonObject.getString("userId");
        return buyService.getAddress(k);
    }

    //全款购买
    @GetMapping("/full/{userid}/{itemid}/{num}")
    @ResponseBody
    public CommonResponse<String> fullPurchase(@PathVariable("userid")String userId,
                                               @PathVariable("itemid") String itemId,
                                               @PathVariable("num") String num)
    {
        int k = Integer.parseInt(num);
        return buyService.fullPurchase(userId,itemId,k);
    }

    //分期购买
    @GetMapping("/instalment/{userid}/{itemid}")
    @ResponseBody
    public CommonResponse<String> instalment(@PathVariable("userid")String userId,
                                             @PathVariable("itemid") String itemId){
        return buyService.instalment(userId,itemId);
    }
    //获取还款列表
    @PostMapping("/repaymentlist")
    @ResponseBody
    public CommonResponse<List<Instalment>> getRepaymentList(@RequestBody String userId){
        JSONObject jsonObject = JSON.parseObject(userId);
        String k = jsonObject.getString("userId");
        return buyService.getRepaymentList(k);
    }
    //还款
    @PostMapping("/repayment")
    @ResponseBody
    public CommonResponse<String> repayment(@RequestBody String id) throws ParseException {
        JSONObject jsonObject = JSON.parseObject(id);
        String k = jsonObject.getString("instalmentId");
        return buyService.repayment(k);
    }
}
